//
//  MANSliderViewController.m
//  Three
//
//  Created by 夜猫子 on 2017/3/22.
//  Copyright © 2017年 夜猫子. All rights reserved.
//

#import "MANSliderViewController.h"

@interface MANSliderViewController ()
@property (nonatomic,strong)UIPanGestureRecognizer *pan;

@property (nonatomic,strong)UITapGestureRecognizer *tap;
@end

@implementation MANSliderViewController

/**
 实现左右控制器的切换
 */
-(instancetype)initWithLeftVC:(UIViewController *)leftVC andRightVC:(UIViewController *)rightVC{
    
    if (self = [super init]) {
        
        //这个时候就有传进来的左侧控制器，和右侧控制器了
        [self addChildViewController:leftVC];
        [self addChildViewController:rightVC];
        
        //2.设置把子控制器的根视图作为我们根视图的子控件
        [self.view addSubview:leftVC.view];
        [self.view addSubview:rightVC.view];
        
        //3.调用didMoveToParentViewController
        [leftVC didMoveToParentViewController:self];
        [rightVC didMoveToParentViewController:self];
        
        //记录左右控制器
        _leftVC = leftVC;
        _rightVC = rightVC;
        
        
    }
    
    return self;
}

//loadView方法一走完就来调用
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //给当前控制器根视图添加拖拽手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(slideRightView:)];
    _pan = pan;
    
    [self.view addGestureRecognizer:pan];
}


#pragma mark - 点按右侧控制器触发
-(void)back:(UITapGestureRecognizer *)tap{
    
    [self closeLeftVC];
}

#pragma mark - 拖拽时触发
-(void)slideRightView:(UIPanGestureRecognizer *)pan{
    
    if (![self isTopviewController]) {
        return;
    }
    
    //取出拖拽偏移量
    CGPoint offset = [pan translationInView:self.view];
    
    //清0
    [pan setTranslation:CGPointZero inView:self.view];
    
    //防止右侧穿帮
    if(offset.x + _rightVC.view.frame.origin.x < 0){
        
        //避免拖动太猛会有间隙，写代码让它回到初始位置
        _rightVC.view.transform = CGAffineTransformIdentity;
        
        return;
    }
    
    //获取当前根视图的宽度
    CGFloat width = self.view.bounds.size.width;
    
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
        case UIGestureRecognizerStateChanged:
            
            //访问右侧控制器
            _rightVC.view.transform = CGAffineTransformTranslate(_rightVC.view.transform, offset.x, 0);
            
            break;
            
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            
            //MARK: 滑动结束超过一半的情况
            //判断有没有超过一半
            if(_rightVC.view.frame.origin.x >= width * 0.5){
                
                [self showLeftWithWidth:width];
                
            }else{
                
                [self closeLeftVC];
            }
            
        default:
            break;
    }
    
}

#pragma mark - 关闭左侧控制器
-(void)closeLeftVC{
    
    [UIView animateWithDuration:0.4 animations:^{
        
        _rightVC.view.transform = CGAffineTransformIdentity;
    }];
    
    [_rightVC.view removeGestureRecognizer:_tap];
    [self.view addGestureRecognizer:_pan];
}

#pragma mark - 显示左侧控制器
-(void)showLeftWithWidth:(CGFloat)width{
    
    [UIView animateWithDuration:0.4 animations:^{
        
        _rightVC.view.transform = CGAffineTransformMakeTranslation(width - 64, 0);
    }completion:^(BOOL finished) {
        
        [self.view removeGestureRecognizer:_pan];
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back:)];
    
    //添加到右侧控制器的View
    [_rightVC.view addGestureRecognizer:tap];
    
    //记录tap属性
    _tap = tap;
}


#pragma mark - 判断是否为首页
- (BOOL)isTopviewController {
    
    UITabBarController *rightVC = (UITabBarController *)_rightVC;
    UINavigationController *nav = rightVC.selectedViewController;
    if (nav.viewControllers.count > 1) {
        return NO;
    }else {
        return YES;
    }
}


@end
